using System;
using System.Collections.Generic;
using System.Resources;
using PluginFrame;

namespace PcrExperimentProject
{
	[Serializable]
	public class AbsolutePcrExperimentProject : PcrExperimentProject
	{
		public AbsolutePcrExperimentProject(IApplication app)
		{
			SetApplication(app);
			mSystemType = PcrSystemType.AbsoluteAnalysis;
			mZonePasswordStatus = new bool[Enum.GetNames(typeof(ProjectPasswordEnum)).Length];
			mResourceManager = ResourceManager.CreateFileBasedResourceManager("PcrExperimentProject", app.GetResourceFolder() + GetType().Namespace + "\\", null);
			mGuid = Guid.NewGuid();
			mFileName = string.Empty;
			mProjectItems = new IProjectItem[mConfigurationService.ChannelCount];
			mChannelConfigurations = new IChannelConfiguration[mConfigurationService.ChannelCount];
			mIsChannelUsed = new List<bool>(mConfigurationService.ChannelCount);
			mAnalysisParameterConfigurationList = new List<IAnalysisParameterConfiguration>(mIsChannelUsed.Count);
			mProjectPropertyList = new List<IProjectProperty>(mIsChannelUsed.Count);
			mCells = new List<IPcrCell>();
			mCrossTalkValue = new double[mConfigurationService.ChannelCount, mConfigurationService.ChannelCount];
			mUnit = mConfigurationService.QuantitativeResultUnit;
			mAvailableChannels = new List<int>(mIsChannelUsed.Count);
			for (int i = 0; i < mConfigurationService.ChannelCount; i++)
			{
				mChannelConfigurations[i] = mConfigurationService.ChannelConfiguration(i);
				mIsChannelUsed.Add(mChannelConfigurations[i].IsSelected);
				mAvailableChannels.Add(i);
				IAnalysisParameterConfiguration analysisParameterConfiguration = mConfigurationService.CreateAbsoluteAnalysisParameterConfiguration(mResourceManager.GetString("Channel") + (i + 1), 0, this) as IAnalysisParameterConfiguration;
				analysisParameterConfiguration.ProjectChannel = i + 1;
				mAnalysisParameterConfigurationList.Add(analysisParameterConfiguration);
				mProjectPropertyList.Add(mConfigurationService.CreateAbsoluteProjectProperty(string.Empty, string.Empty, mConfigurationService.QuantitativeThreshold, mConfigurationService.QualitativeThreshold, mConfigurationService.QualitativeResultUnit, mConfigurationService.QuantitativeResultUnit, false, false, 0.0, 0.0));
			}
			mPcrProgram = new PcrProgramStructure(app, this);
			mTemperatureControlType = mConfigurationService.DefaultTemperatureControlType;
			mAdvancedRule = mRuleEditorService.CreateRuleObject();
		}

		protected virtual void AddNewProjectTarget(string targetName, IProjectItem projectItem, int targetIndex)
		{
			IAnalysisParameterConfiguration analysisParameterConfiguration = mConfigurationService.CreateAbsoluteAnalysisParameterConfiguration(targetName, targetIndex, this) as IAnalysisParameterConfiguration;
			analysisParameterConfiguration.ProjectProperty = mConfigurationService.CreateAbsoluteProjectProperty(targetName, string.Empty, mConfigurationService.QuantitativeThreshold, mConfigurationService.QualitativeThreshold, mConfigurationService.QualitativeResultUnit, mConfigurationService.QuantitativeResultUnit, false, false, 0.0, 0.0);
			projectItem.TubeAnalysisParameterConfiguration.Add(analysisParameterConfiguration);
			IAnalysisParameterConfiguration item = CreateMeltingAnalysisParameterConfiguration(targetName, targetIndex);
			projectItem.TubeMeltingAnalysisParameterConfiguration.Add(item);
		}

		public override IProjectItem CreateProjectItem(int index, string name, int tubeCount)
		{
			IProjectItem projectItem = null;
			if (mProjectItems[index] == null)
			{
				projectItem = new AbsoluteProjectItem(name);
				projectItem.Dye = mChannelConfigurations[index].Content;
				projectItem.ChannelConfiguration = mChannelConfigurations[index];
				projectItem.AnalysisParameterConfiguration = mConfigurationService.CreateAbsoluteAnalysisParameterConfiguration(name, 0, this) as IAnalysisParameterConfiguration;
				if (string.IsNullOrEmpty(name))
				{
					projectItem.TubeAnalysisParameterConfiguration = new List<IAnalysisParameterConfiguration> { projectItem.AnalysisParameterConfiguration };
					IAnalysisParameterConfiguration item = CreateMeltingAnalysisParameterConfiguration(name, 0);
					projectItem.TubeMeltingAnalysisParameterConfiguration = new List<IAnalysisParameterConfiguration> { item };
				}
				else
				{
					projectItem.TubeAnalysisParameterConfiguration = new List<IAnalysisParameterConfiguration>();
					projectItem.TubeMeltingAnalysisParameterConfiguration = new List<IAnalysisParameterConfiguration>();
					string[] array = name.Split(new string[1] { "," }, StringSplitOptions.None);
					for (int i = 0; i < tubeCount; i++)
					{
						AddNewProjectTarget(array[i], projectItem, i);
					}
				}
				mProjectItems[index] = projectItem;
			}
			else
			{
				ChangeProjectItemName(index, name);
				projectItem = mProjectItems[index];
				string[] array2 = name.Split(new string[1] { "," }, StringSplitOptions.None);
				if (tubeCount < projectItem.TubeAnalysisParameterConfiguration.Count)
				{
					for (int j = 0; j < tubeCount; j++)
					{
						if (projectItem.TubeAnalysisParameterConfiguration.Count <= j)
						{
							projectItem.TubeAnalysisParameterConfiguration.Add(projectItem.AnalysisParameterConfiguration);
						}
						projectItem.TubeAnalysisParameterConfiguration[j].ProjectName = array2[j];
						if (projectItem.TubeMeltingAnalysisParameterConfiguration.Count <= j)
						{
							projectItem.TubeMeltingAnalysisParameterConfiguration.Add(CreateMeltingAnalysisParameterConfiguration(array2[j], 0));
						}
						else
						{
							projectItem.TubeMeltingAnalysisParameterConfiguration[j].ProjectName = array2[j];
						}
					}
					projectItem.TubeAnalysisParameterConfiguration.RemoveRange(tubeCount, projectItem.TubeAnalysisParameterConfiguration.Count - tubeCount);
					projectItem.TubeMeltingAnalysisParameterConfiguration.RemoveRange(tubeCount, projectItem.TubeMeltingAnalysisParameterConfiguration.Count - tubeCount);
				}
				else
				{
					for (int k = 0; k < projectItem.TubeAnalysisParameterConfiguration.Count; k++)
					{
						if (projectItem.TubeAnalysisParameterConfiguration.Count <= k)
						{
							projectItem.TubeAnalysisParameterConfiguration.Add(projectItem.AnalysisParameterConfiguration);
						}
						projectItem.TubeAnalysisParameterConfiguration[k].ProjectName = array2[k];
						if (projectItem.TubeMeltingAnalysisParameterConfiguration.Count <= k)
						{
							projectItem.TubeMeltingAnalysisParameterConfiguration.Add(CreateMeltingAnalysisParameterConfiguration(array2[k], 0));
						}
						else
						{
							projectItem.TubeMeltingAnalysisParameterConfiguration[k].ProjectName = array2[k];
						}
					}
					for (int l = projectItem.TubeAnalysisParameterConfiguration.Count; l < tubeCount; l++)
					{
						AddNewProjectTarget(array2[l], projectItem, l);
					}
				}
			}
			return projectItem;
		}

		protected virtual IAnalysisParameterConfiguration CreateMeltingAnalysisParameterConfiguration(string name, int targetIndex)
		{
			return mConfigurationService.CreateMeltingAnalysisParameterConfiguration(name, mConfigurationService.PcrProgramConfiguration.DefaultMeltingStartTemperature, mConfigurationService.PcrProgramConfiguration.DefaultMeltingStopTemperature, 5.0, 0.1, MeltingAnalysisType.CustomGroup, PcrSystemType.AbsoluteAnalysis, this, targetIndex) as IAnalysisParameterConfiguration;
		}

		public override IProjectItem ChangeProjectItemName(int index, string name)
		{
			if (index >= 0 && index < mProjectItems.Length && mProjectItems[index] != null)
			{
				mProjectItems[index].Name = name;
			}
			return mProjectItems[index];
		}

		public AbsolutePcrExperimentProject(IApplication app, List<bool> isChannelUsed)
			: this(app)
		{
		}
	}
}
